﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MissionCenter
{
    /// <summary>
    /// MarketBlock.xaml 的交互逻辑
    /// </summary>
    public partial class MarketBlock : UserControl
    {
        public JToken Information=null;
        public string Api = "";
        public string Token = "";
        public JObject UserInfor = null;
        public MarketBlock(string api,string token, JObject userInfor)
        {
            Api = api;
            Token = token;
            UserInfor = userInfor;
            InitializeComponent();
        }

        private void scriptAuthor_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(Information);
        
            e.Handled = true;
        }

        private async void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("大图标点击");            
            string scriptData = await DataCenter.RemoteDate.GetScript(Api, Token, int.Parse(Information["script_id"].ToString()));
            if (scriptData != null)
            {
                // 成功获取到数据，进行后续操作
                Console.WriteLine(JsonConvert.DeserializeObject<JObject>(scriptData));
                DataCenter.LocalDataManage.scriptLocalWrite($"users/{UserInfor["username"]}/userdata.db",JsonConvert.DeserializeObject<JObject>(scriptData));
            }
            else
            {
                // 处理获取数据失败的情况
                Console.WriteLine("Failed to get script data");
            }

        }
    }
}
